package com.pwser.controller;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

@Controller
public class GongfuController {
	private final String PREFIX = "pages/";

	@GetMapping("/")
	public String index() {
		return "welcome";
	}

	@GetMapping("/main")
	public String main(HttpServletRequest request, HttpServletResponse response) {
		Cookie[] cookies = request.getCookies();
		String token = null;
		for (Cookie cookie : cookies) {
			switch (cookie.getName()) {
			case "Authorization":
				token = cookie.getValue();
				break;
			default:
				break;
			}
		}

		if (token != null) {
			response.setHeader("Authorization", "token:" + token);
		}
		return "welcome";
	}

	/**
	 * level1页面映射
	 * 
	 * @param path
	 * @return
	 */
	@GetMapping("/level1/{path}")
	public String level1(@PathVariable("path") String path) {
		return PREFIX + "level1/" + path;
	}

	/**
	 * level2页面映射
	 * 
	 * @param path
	 * @return
	 */
	@GetMapping("/level2/{path}")
	public String level2(@PathVariable("path") String path) {
		return PREFIX + "level2/" + path;
	}

	/**
	 * level3页面映射
	 * 
	 * @param path
	 * @return
	 */
	@GetMapping("/level3/{path}")
	public String level3(@PathVariable("path") String path) {
		return PREFIX + "level3/" + path;
	}

}
